package yl.hs.bmipfull.controllers.report;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.service.report.IProductionDayReportService;
import yl.hs.bmipfull.service.system.ISearchViewService;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.WebResult;

import java.sql.Time;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/*
 * ProductionReportController
 * @category
 * @author 谢景广
 * @date 2023-02-02
 */
@Api(value = "生产日报报表web接口",tags = "报表中心——生产日报")
@RestController
@RequestMapping(value = "/api/production/report/day")
public class ProductionReportController extends ControllerBase {

    @ApiOperation(value = "查询生产日报信息", notes = "查询生产日报信息")
    @PostMapping(value = "query/{rowNum}/{page}")
    public WebResult queryList(@RequestBody PageQuery pageQuery,@PathVariable int rowNum,@PathVariable int page){
        try{
            svSVC.updateUserMenuView(pageQuery);
            return success("查询成功", service.queryList(pageQuery, rowNum, page));
        }
        catch (Exception ex){
            return error(ex);
        }
    }
    @ApiOperation(value = "查询生产日报信息", notes = "查询生产日报信息")
    @PutMapping(value = "generate/{productCateSN}/{day}/{unitName}")
    public WebResult generateReport(@PathVariable String productCateSN, @PathVariable String day,@PathVariable String unitName){
        try{
            return success("报表已生成",service.generateReport(Tl.strToDate(day),unitName,productCateSN));
        }
        catch (Exception ex){
            return error(ex);
        }
    }
    @ApiOperation(value = "各标号生产成本明细表", notes = "各标号生产成本明细表")
    @GetMapping(value = {"productCostByDay/{time}","productCostByDay/{time}/{unit}"})
    public WebResult queryReportTasks(@PathVariable String time,@PathVariable(required = false) String unit){
        try{
            return success("ok",service.productCostByDay(new SimpleDateFormat("yyyy-MM-dd").parse(time),unit));
        }
        catch (Exception ex){
            return error(ex);
        }
    }
    @ApiOperation(value = "查询生产日报任务信息", notes = "查询生产日报任务信息")
    @GetMapping(value = "tasks/{sn}")
    public WebResult queryReportTasks(@PathVariable String sn){
        try{
            return success("ok",service.queryReportTasks(sn));
        }
        catch (Exception ex){
            return error(ex);
        }
    }
    @ApiOperation(value = "查询生产日报原材信息", notes = "查询生产日报原材信息")
    @GetMapping(value = "materialsCombination/{sn}")
    public WebResult queryReportMaterialsCombination(@PathVariable String sn){
        try{
            return success("ok",service.queryReportMaterialsCombination(sn));
        }
        catch (Exception ex){
            return error(ex);
        }
    }
    @ApiOperation(value = "查询生产日报原材信息", notes = "查询生产日报原材信息")
    @GetMapping(value = "materialsList/{sn}")
    public WebResult queryReportMaterialsList(@PathVariable String sn){
        try{
            return success("ok",service.queryReportMaterialsList(sn));
        }
        catch (Exception ex){
            return error(ex);
        }
    }
    @ApiOperation(value = "查询生产日报中任务的发货单信息", notes = "查询生产日报原材信息")
    @GetMapping(value = "orders/{sn}/{taskSN}")
    public WebResult queryReportOrders(@PathVariable String sn,@PathVariable String taskSN){
        try{
            return success("ok",service.queryReportOrders(sn,taskSN));
        }
        catch (Exception ex){
            return error(ex);
        }
    }
    @ApiOperation(value = "生成所有站点昨天的生产日报", notes = "生成所有站点昨天的生产日报")
    @GetMapping(value = "all/{time}")
    public WebResult generateReportAll(@PathVariable  Date time){
        try{
            service.autoGenerateReport(time);
            return success("ok");
        }
        catch (Exception ex){
            return error(ex);
        }
    }
    @ApiOperation(value = "查询生产日报生产线生产量发货量", notes = "查询生产日报生产线生产量发货量")
    @GetMapping(value = "lines/{sn}")
    public WebResult queryReportLines(@PathVariable String sn){
        try{
            return success("ok",service.queryReportLines(sn));
        }
        catch (Exception ex){
            return error(ex);
        }
    }
    @ApiOperation(value = "添加原材消耗入库统计信息", notes = "添加原材消耗入库统计信息")
    @PostMapping("add/material")
    public WebResult addMaterialInfo(@RequestBody SubmitModel model){
        try {
            return success("原材信息已提交",service.addMaterialInfo(model));
        }
        catch (Exception ex){
            return error(ex);
        }
    }
    @ApiOperation(value = "保存调整后的报表信息", notes = "保存调整后的报表信息")
    @PostMapping("save")
    public WebResult saveReportInfo(@RequestBody SubmitModel model){
        try {
            return success("原材信息已提交",service.saveReportInfo(model));
        }
        catch (Exception ex){
            return error(ex);
        }
    }

    private ISearchViewService svSVC;

    @Autowired
    public void setSvSVC(ISearchViewService svSVC) {
        this.svSVC = svSVC;
    }

    @Autowired
    private IProductionDayReportService service;

    @Autowired
    public void setService(IProductionDayReportService service) {
        this.service = service;
    }
}
